package com.parttimetask.activities;
import android.annotation.SuppressLint;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.parttimetask.App;
import com.parttimetask.databinding.ActivitySignBinding;
import com.parttimetask.utils.Http;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
public class SignActivity extends AppCompatActivity {
    private ActivitySignBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding= ActivitySignBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        setResult(4);
        binding.signGender.setOnClickListener(view -> {
            AlertDialog.Builder dialog=new AlertDialog.Builder(SignActivity.this);
            dialog.setItems(new String[]{"Male","Female"}, (dialogInterface, i) -> {
                if (i==0){
                    binding.signGender.setText("Male");
                }
                if (i==1){
                    binding.signGender.setText("Female");
                }
            }).show();
        });
        binding.signRegister.setOnClickListener(view -> {
          if (binding.signName.getText().toString().equals("")||
                  binding.signMobile.getText().toString().equals("")||
                  binding.signPassword.getText().toString().equals("")||
                  binding.signAge.getText().toString().equals("")||
                  binding.signGender.getText().toString().equals("")){
              Toast.makeText(SignActivity.this, "Please input completely", Toast.LENGTH_SHORT).show();
              return;
          }
            try {
                JSONObject jo=new JSONObject();
                jo.put("name",binding.signName.getText().toString());
                jo.put("mobile",binding.signMobile.getText().toString());
                jo.put("password",binding.signPassword.getText().toString());
                jo.put("age",binding.signAge.getText().toString());
                jo.put("gender",binding.signGender.getText().toString());
                Http.post(jo, "user/sign/", new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        e.printStackTrace();
                    }
                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) {
                        runOnUiThread(() -> {
                            try {
                                String res = response.body().string();
                                if (res.equals("创建成功")){
                                    Toast.makeText(SignActivity.this, "Register successful", Toast.LENGTH_SHORT).show();
                                    @SuppressLint("CommitPrefEdits") SharedPreferences.Editor edit = App.sp.edit();
                                    edit.putString("mobile",binding.signMobile.getText().toString());
                                    edit.putString("password",binding.signPassword.getText().toString());
                                    edit.apply();
                                    setResult(5);
                                    finish();
                                }else {
                                    Toast.makeText(SignActivity.this, "Register failed,the email has been registered", Toast.LENGTH_SHORT).show();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        });
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        });
        Http.get("getMobileArea/", new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                e.printStackTrace();
            }
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) {
                runOnUiThread(() -> {
                    try {
                        binding.signNumbercode.setText("+"+response.body().string());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            }
        });
    }
}
